function checkdata() {
if (!checkName(changeinfo.name.value)) return false;
if(!checkaddress(changeinfo.addr.value)) return false;
if(!checkAge(changeinfo.birth_year.value,changeinfo.birth_month.value)) return false;
if (!checkid(changeinfo.id.value)) return false;
if (!checkphone(changeinfo.phone.value)) return false;
return true;
}

function checkName(sn){
	if( sn.length<3 || sn.length>40 ) {
		// alert("\请输入正确的名字,名字长度为3～40位！");	
		document.getElementById("nameErr").innerHTML = "<font color = 'red'>Name's length is 3 to 40！</font>";
		changeinfo.name.focus();
		return false;
	}
	document.getElementById("nameErr").innerHTML = "";
	return true;
}

function checkid(userid){
	if( userid.length != 8) {
		//alert("\请输入正确的id,id长度为8位！");
		document.getElementById("idErr").innerHTML = "<font color = 'red'>id's length must be 8！</font>";
		changeinfo.id.focus();
		return false;
	}
	if (isWhiteWpace(userid)){
		//alert("\请输入正确的id,id中不能包含空格！");
		document.getElementById("idErr").innerHTML = "<font color = 'red'>id cannot contains space！</font>";
		changeinfo.id.focus();
		return false;
	}
	for(a = 0;a<userid.length;a++) {
		if(userid.charAt(a) > '9' || userid.charAt(a) <'0' ) {
			//alert("\请输入正确的id,id中只能為數字！");
			document.getElementById("idErr").innerHTML = "<font color = 'red'>id must be number！</font>";
			changeinfo.id.focus();
			return false;
		}
	}
	document.getElementById("idErr").innerHTML = "";
	return true;
}


function checkAge(year,month){ 
	optionclean(document.changeinfo.birth_day);
	switch (month) {
	case '2' :  if(checkLeapYear(year)) {
					  document.changeinfo.birth_day.options.add(new Option(29));
					 //document.getElementById("birthErr").innerHTML = "<font color = 'red'>該年2月只有29天！</font>";
			  	} 
			 	break;
	case '4' :
	case '6' :
	case '9' :
	case '11': 	document.changeinfo.birth_day.options.add(new Option(29));
				//document.getElementById("birthErr").innerHTML = "<font color = 'red'>該月只有30天！</font>";
				document.changeinfo.birth_day.options.add(new Option(30));
				break;
	default : 	document.changeinfo.birth_day.options.add(new Option(29));
				document.changeinfo.birth_day.options.add(new Option(30));
				document.changeinfo.birth_day.options.add(new Option(31));
	}
	//document.getElementById("birthErr").innerHTML = "";
	return true;
}
		
function checkphone(pphone){
	if( pphone.length != 8) {
		//alert("\请输入正确的phone,phone长度为8位！");
		document.getElementById("phoneErr").innerHTML = "<font color = 'red'>Phone's length must be 8！</font>";
		changeinfo.phone.focus();
		return false;
	}
	if (isWhiteWpace(pphone)){
		//alert("\请输入正确的phone,phone中不能包含空格！");
		document.getElementById("phoneErr").innerHTML = "<font color = 'red'>Phone cannot contains space！</font>";
		changeinfo.phone.focus();
		return false;
	}
	for(b = 0;b<pphone.length;b++) {
		if(pphone.charAt(b) > '9' || pphone.charAt(b) <'0' ) {
			//alert("\请输入正确的phone,phone中只能為數字！");
			document.getElementById("phoneErr").innerHTML = "<font color = 'red'>Phone must be number！</font>";
			changeinfo.phone.focus();
			return false;
		}
	}
	document.getElementById("phoneErr").innerHTML = "";
	return true;
}

function checkaddress(address){
	if( address.length<3 || address.length>40 ) {
		//alert("\请输入正确的Address,Address长度为3～40位！");
		document.getElementById("addressErr").innerHTML = "<font color = 'red'>Address's length is 3 to 40！</font>";
		changeinfo.addr.focus();
		return false;
	}
	if (isWhiteWpace(address)){
		//alert("\请输入正确的Address,Address中不能包含空格！");
		document.getElementById("addressErr").innerHTML = "<font color = 'red'>Address cannot contains space！</font>";
		changeinfo.addr.focus();
		return false;
	}
	if (!isSsnString(address)){
		//alert("\对不起，您选择的Address不正确！用户名\n由a～z的英文字母(不区分大小写)、0～9的数字、点、减\n号或下划线组成，长度为3～40个字符，只能以数字或字母\n开头和结尾,例如：beijing2008，beijing.2008。");
		document.getElementById("addressErr").innerHTML = "<font color = 'red'>Address contains illegal characters！</font>";
		changeinfo.addr.focus();
		return false;
	}
	document.getElementById("addressErr").innerHTML = "";
	return true;
}
	
function strlen(str){
	var len;
	var i;
	len = 0;
	for (i=0;i<str.length;i++){
		if (str.charCodeAt(i)>255) len+=2; else len++;
	}
	return len;
}
	
function strlen2(str){
	var len;
	var i;
	len = 0;
	for (i=0;i<str.length;i++){
		if (str.charCodeAt(i)>255) return true;
	}
	return false;
}

function isWhiteWpace (s)
{
	var whitespace = " \t\n\r";
	var i;
	for (i = 0; i < s.length; i++){   
		var c = s.charAt(i);
		if (whitespace.indexOf(c) >= 0) {
			return true;
		}
	}
	return false;
}
	
function isSsnString (ssn)
{
	var re=/^[0-9a-z][\w-.]*[0-9a-z]$/i;
	if(re.test(ssn))
		return true;
	else
		return false;
}

function checkLeapYear (year) {
	if(year%4 == 0) {
		return true;
	}	
	return false;
}

function optionclean(obj)
{
	obj.options.length=28;
}
